package com.github.kezhenxu94.playground.java.oj.projecteuler;

/**
 * Problem 3
 * 
 * Largest prime factor
 * 
 * The prime factors of 13195 are 5, 7, 13 and 29.
 * 
 * What is the largest prime factor of the number 600851475143 ?
 * 
 * @author ke.zhen.xu
 *
 */
public class Solution002LargestPrimeFactor {

	public static void main(String[] args) {
		long testCase = 600851475143L;
		for (long i = testCase; i > 2; i -= 2)
			if (isPrime(i) && testCase % i == 0) {
				System.out.println(i);
				break;
			}
	}

	private static boolean isPrime(long num) {
		if (num == 2)
			return true;
		if (num % 2 == 0)
			return false;
		for (long i = 3; i * i < num; i += 2)
			if (num % i == 0)
				return false;
		return true;
	}
}
